<template>
    <div ref="body">
        <Form :label-width="100" inline>
            <Form-item label="区/县:">
                <dictSelect :sel_val='queryParam.ssfc0001' dicType="service"
                            dictService="/api/ac/subsistence/extSysDictService/getSs01001List" clearable
                            :bindData.sync="queryParam.ssfc0001" class="itemShortStyle"/>
            </Form-item>
            <Form-item label="发放年月:">
                <DatePicker type="month" v-model="queryParam.ssfc0002" class="itemShortStyle"
                            format="yyyy-MM" :editable="false"></DatePicker>
            </Form-item>
            <Form-item label="救助对象类别:">
                <Select v-model="queryParam.ssfc0004" class="itemShortStyle" clearable>
                    <Option value="1010">城市最低生活保障对象</Option>
                    <Option value="1020">城市低保边缘家庭</Option>
                    <Option value="1030">农村最低生活保障对象</Option>
                    <Option value="1040">农村低保边缘家庭</Option>
                    <Option value="1050">城市单人保障对象</Option>
                    <Option value="1060">城市因病致贫保障对象</Option>
                    <Option value="1070">农村单人保障对象</Option>
                    <Option value="1080">农村因病致贫保障对象</Option>
                    <Option value="1090">农村建档立卡保障对象</Option>
                </Select>
            </Form-item>
            <Form-item label="生成是否成功:">
                <dictSelect :sel_val='queryParam.ssfc0008' dictCode="yesorno" :bindData.sync="queryParam.ssfc0008"
                            class="itemShortStyle" clearable/>
            </Form-item>
            <Form-item label="查验状态:">
                <dictSelect :sel_val='queryParam.ssfc0015' dictCode="ssfc0015"
                            :bindData.sync="queryParam.ssfc0015" class="itemShortStyle" clearable/>
            </Form-item>
            <Form-item label="是否确认:">
                <dictSelect :sel_val='queryParam.ssfc0010' dictCode="yesorno" :bindData.sync="queryParam.ssfc0010"
                            class="itemShortStyle" clearable/>
            </Form-item>
            <Row class="button_row">
               <span style="float: left;">
            <Button type="primary" @click="handleAdd()">新建</Button>
             <Button type="primary" class="button_item_left" @click="handleEdit()">编辑</Button>
             <Button type="primary" class="button_item_left" @click="handleDeleteSs18900()">删除</Button>
             <Button type="primary" class="button_item_left" @click="handleDetail()">查看</Button>
             <Button type="primary" class="button_item_left" @click="generateRoster()">花名册生成</Button>
             <Button type="primary" class="button_item_left" @click="rostercheck()">花名册查验</Button>
             <Button type="primary" class="button_item_left" @click="rosterConfirm()">花名册确认</Button>
             <Button type="primary" class="button_item_left" @click="validateDetail()">查验异常详情</Button>
            </span>
                <span style="float: right;">
                    <Button type="primary" @click="searchQuery18900">查询</Button>
                    <Button type="primary" class="button_item_left" @click="searchReset">重置</Button>
             </span>
            </Row>
        </Form>

        <Modal v-model="noBankInfo" title="银行账户信息未录入完整" width="980" style="position:relative;">
            <div>
                <Table :columns="columnsNoBankInfo" :data="noBankInfoData" :loading="Loading" border></Table>
                <Page :total="dataCountNoBankInfo" :page-size="pageSize" show-total class="paging"
                      @on-change="handleTableChange" :current="pageNo"></Page>
            </div>
            <br>
            <br>
            <div slot="footer">
                <Button type="text" size="large" @click="noBankInfo = false">取消</Button>
            </div>
        </Modal>
        <div class="table_div">
            <vxe-table border
                       resizable
                       row-id="id"
                       :row-key="true"
                       show-overflow="tooltip"
                       align="center"
                       header-align="center"
                       ref="xTree"
                       :loading="Loading"
                       :radio-config="{highlight: true}"
                       @cell-click="selectChange"
                       :data="tableData">
                <vxe-table-column type="radio" width="60" align="center" fixed="left"/>
                <vxe-table-column field="ssfc0002" title="发放年月" align="center" width="120" fixed="left"
                                  :formatter="Time1"/>
                <vxe-table-column field="azaa0002" title="区县名称" align="center" width="100" fixed="left"/>
                <vxe-table-column field="ssfc0004_name" title="救助对象类别" align="center" width="200" fixed="left"/>
                <vxe-table-column field="ssfc0005_name" title="是否生成" align="center" width="80"/>
                <vxe-table-column field="ssfc0006" title="生成时间" align="center" width="120" :formatter="renderTime"/>
                <vxe-table-column field="ssfc0007" title="生成人姓名" align="center" width="140"/>
                <vxe-table-column field="ssfc0008_name" title="生成是否成功" align="center" width="110"/>
                <vxe-table-column field="ssfc0013" title="查验申请人" align="center" width="110"/>
                <vxe-table-column field="ssfc0014" title="查验计划时间" align="center" width="120" :formatter="renderTime"/>
                <vxe-table-column field="ssfc0015_name" title="查验状态" align="center" width="140"/>
                <vxe-table-column field="ssfc0010_name" title="是否确认" align="center" width="80"/>
                <vxe-table-column field="ssfc0012" title="确认时间" align="center" width="120" :formatter="renderTime"/>
                <vxe-table-column field="ssfc0011" title="确认人" align="center" width="120"/>
                <vxe-table-column field="" title="操作" min-width="130" fixed="right">
                    <template v-slot="{ row }">
                        <Button v-if="row.ssfc0015_name=='查验未通过'" type="error" style="margin-right: 5px"
                                @click="searchErrorInfo(row)">查验异常处理
                        </Button>
                    </template>
                </vxe-table-column>
            </vxe-table>
            <Page :total="dataCount" :page-size="pageSize" show-total class="paging"
                  @on-change="handleTableChange" :current="pageNo"></Page>
        </div>
        <repeat-detail ref="repeatDetailModal"/>
        <roster-model ref="rosterModel"/>
        <check-plan-model ref="checkPlanModel"/>
        <exception-handle-model ref="exceptionHandleModel"/>
        <exception-show-model ref="exceptionShowModel"/>
    </div>
</template>

<script>
  import { postAction } from '@/api/request'
  import { CommonMixin } from '@/views/common/js/CommonMixin'
  import dictSelect from '@/components/dictSelect/dictSelect.vue'
  import DateFormat from '@/plugins/format.js'
  import repeatDetail from './modules/repeatDetail'
  import rosterModel from './modules/rosterModel'
  import checkPlanModel from './modules/checkPlanSetModel'
  import exceptionHandleModel from './modules/exceptionHandleModel'
  import exceptionShowModel from './modules/exceptionShowModel'

  export default {
    mixins: [CommonMixin],
    components: {
      rosterModel,
      dictSelect,
      checkPlanModel,
      exceptionHandleModel,
      exceptionShowModel,
      repeatDetail
    },
    data () {
      return {
        noBankInfo: false,
        validateInfo: false,
        noBankInfoData: [],
        options: [],
        columns: [
          {
            title: '发放年月',
            align: 'center',
            key: 'ssfc0002',
            render: (h, params) => {
              let value = params.row.ssfc0002
              if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                value = '--'
              } else {
                value = DateFormat(value, 'YYYY-MM')
              }
              return h('span', value + ' ')
            },
            width: 100,
            fixed: 'left'
          },
          {
            title: '区县名称',
            align: 'center',
            key: 'azaa0002',
            width: 100,
            fixed: 'left'
          },
          {
            title: '救助对象类别',
            align: 'center',
            key: 'ssfc0004_name',
            width: 200,
            fixed: 'left'
          },
          {
            title: '是否生成',
            align: 'center',
            key: 'ssfc0005_name',
            width: 100,
          },
          {
            title: '生成时间',
            align: 'center',
            key: 'ssfc0006',
            width: 120,
            render: (h, params) => {
              let value = params.row.ssfc0006
              if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                value = '--'
              } else {
                value = DateFormat(value, 'YYYY-MM-DD')
              }
              return h('span', value + ' ')
            },
          },
          {
            title: '生成人姓名',
            align: 'center',
            key: 'ssfc0007',
            width: 120,
          },
          {
            title: '生成是否成功',
            align: 'center',
            key: 'ssfc0008_name',
            width: 120
          },
          {
            title: '查验申请人',
            align: 'center',
            key: 'ssfc0013',
            width: 120,
          },
          {
            title: '查验计划时间',
            align: 'center',
            key: 'ssfc0014',
            width: 120,
            render: (h, params) => {
              let value = params.row.ssfc0014
              if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                value = '--'
              } else {
                value = DateFormat(value, 'YYYY-MM-DD')
              }
              return h('span', value + ' ')
            },
          },
          {
            title: '查验状态',
            align: 'center',
            key: 'ssfc0015_name',
            width: 120,
          },
          {
            title: '是否确认',
            align: 'center',
            key: 'ssfc0010_name',
            width: 100,
          },
          {
            title: '确认时间',
            align: 'center',
            key: 'ssfc0012',
            width: 120,
            render: (h, params) => {
              let value = params.row.ssfc0012
              if (typeof (value) === 'undefined' || value === null || value === '' || value === undefined) {
                value = '--'
              } else {
                value = DateFormat(value, 'YYYY-MM-DD')
              }
              return h('span', value + ' ')
            },
          },
          {
            title: '确认人',
            align: 'center',
            key: 'ssfc0011',
            width: 100,
          },
          {
            title: '操作',
            key: 'action',
            align: 'center',
            width: 100,
            slot: 'action',
            fixed: 'right'
          }
        ],
        ss18900Id: '',
        columnsNoBankInfo: [
          {
            title: '区划',
            align: 'center',
            key: 'azaa0002',
            // width: 300,
          },
          {
            title: '审批救助类型',
            align: 'center',
            key: 'type',
            // width: 200,
          },
          {
            title: '户主姓名',
            align: 'center',
            key: 'azcp0003',
            // width: 200,
          },
          {
            title: '户主身份证号',
            align: 'center',
            key: 'azcp0001',
            // width: 200,
          }
        ],
        tableData: [],
        Loading: false,
        url: {
          list: '/api/ac/subsistence/ss18900Service/getPageList',
          delete: '/api/ac/subsistence/ss18900Service/deleteData',
          generate: '/api/ac/subsistence/ss18900Service/updateAndCreateRosterData',
        },
        queryParam: {},
        results: {},
        visible: false,
        dataCountNoBankInfo: 0,
        modal: false,
        title: '',
        selectedData: '',
        selectRow: null,
      }
    },
    methods: {

      //查验异常名单
      searchErrorInfo (row) {
        this.$refs.exceptionHandleModel.edit(row)
        this.$refs.exceptionHandleModel.key = this.uuid()
      },
      //月度花名册例外详情
      validateDetail () {
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        }
        let record = this.selectedData
        this.$refs.exceptionShowModel.edit(record)
        this.$refs.exceptionShowModel.key = this.uuid()
      },
      selectChange ({ row }) {
        this.$refs.xTree.setRadioRow(row)
        this.selectedData = row
      },
      //花名册事后查验
      rosterExe () {
        var dataParam = {}
        postAction('/api/ac/subsistence/ss00005Service/createSs18900BetaCheck', dataParam).then((res) => {
          this.$Message.success('查验成功！')
        })
      },
      //花名册查验
      rostercheck () {
        let that = this
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        }
        let record = that.selectedData
        if (record.ssfc0005 != '1' || record.ssfc0008 != '1') {
          this.$Message.error('请生成救助花名册，再制定查验计划！')
          return
        } else if (record.ssfc0010 == '1') {
          this.$Message.error('已经确认救助花名册，不允许再次查验！')
          return
        } else if (record.ssfc0015 == '1') {
          this.$Modal.confirm({
            title: '已经通过公安户籍查验的救助对象花名册，确定重新进行查验?',
            onOk: () => {
              let record = that.selectedData
              this.$refs.checkPlanModel.edit(record)
              this.$refs.checkPlanModel.key = this.uuid()
            }
          })
        } else {
          let record = that.selectedData
          this.$refs.checkPlanModel.edit(record)
          this.$refs.checkPlanModel.key = this.uuid()
        }
      },

      generateRoster () {
        let that = this
        if (that.selectedData == '') {
          that.$Message.error('请选择要生成救助花名册的数据！')
          return false
        }
        let row = that.selectedData
        let url = that.url.generate
        let msg = '确定生成此数据的救助对象花名册吗？'
        let ssfc0005 = row.ssfc0005
        if ('1' == ssfc0005) {
          msg = '已经生成此数据的救助对象花名册，确定重新生成吗？'
        }
        if (row.ssfc0010 == '1') {
          this.$Message.error('已经确认救助花名册，不允许再次生成！')
          return
        }
        row.ssfc0002 = DateFormat(row.ssfc0002, 'YYYY-MM')
        that.$Modal.confirm({
          title: msg,
          onOk: () => {
            that.Loading = true
            postAction(url, row).then((res) => {
              if (res.data.errcode != 0) {
                this.$Message.error(res.data.errmsg)
              } else {
                if (res.data.data.alreadyGenerate) {
                  // 当前花名册保障人员是否已在其他区域生成，查看详情
                  this.$refs.repeatDetailModal.edit(row)
                  this.$refs.repeatDetailModal.key = this.uuid()
                }
                else {
                  that.$Message.success(res.data.data.msg)
                  that.loadData()
                }
                this.visible = false
              }
            }).finally(() => {
              that.Loading = false
            })
          },
          onCancel: () => {
            that.Loading = false
          }
        })
      },
      rowSelect (row, index) {
        this.selectedData = row
      },
      handleAdd () {
        let that = this
        let record = {}
        this.$refs.rosterModel.edit(record)
        this.$refs.rosterModel.key = this.uuid()
        this.$refs.rosterModel.title = '花名册录入'
        this.$refs.rosterModel.disableSubmit = false
      },
      handleEdit (index) {
        let that = this
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        } else if (that.selectedData.ssfc0005 == '1') {
          that.$Message.error('已经生成救助花名册，不允许编辑！')
          return
        } else if (that.selectedData.ssfc0010 == '1') {
          that.$Message.error('已经确认救助花名册，不允许编辑！')
          return
        } else {
          let record = that.selectedData
          this.$refs.rosterModel.edit(record)
          this.$refs.rosterModel.key = this.uuid()
          this.$refs.rosterModel.title = '花名册编辑'
          this.$refs.rosterModel.disableSubmit = false
        }
      },
      handleDetail (index) {
        let that = this
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        } else {
          let record = that.selectedData
          this.$refs.rosterModel.edit(record)
          this.$refs.rosterModel.key = this.uuid()
          this.$refs.rosterModel.title = '花名册查看'
          this.$refs.rosterModel.disableSubmit = true
        }
      },

      handleDeleteSs18900 () {
        let that = this
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        } else if (that.selectedData.ssfc0005 == '1') {
          that.$Message.error('已经生成救助花名册，不允许删除！')
          return
        } else if (that.selectedData.ssfc0010 == '1') {
          that.$Message.error('已经确认救助花名册，不允许删除！')
          return
        } else {
          this.$Modal.confirm({
            title: '确定删除吗？',
            onOk: () => {
              if (!this.url.delete) {
                this.$Message.error('请设置url.delete属性!')
                return
              }
              postAction(that.url.delete, that.selectedData).then((res) => {
                if (res.data.data.success == 0) {
                  //重新计算分页问题
                  that.reCalculatePage(1)
                  that.$Message.success('删除成功！')
                } else {
                  that.$Message.error(res.data.errmsg)
                }
              })
            }
          })
        }
      },
      rosterConfirm () {
        let that = this
        let url = '/api/ac/subsistence/ss18900Service/updateAndDoRosterConfirm'
        if (this.selectedData == '') {
          this.$Message.error('请选中一条数据!')
          return false
        } else if (that.selectedData.ssfc0005 != '1') {
          this.$Message.error('未生成救助花名册，不允许进行花名册确认！')
          return
        } else if (that.selectedData.ssfc0010 == '1') {
          this.$Message.error('已经确认救助花名册，不允许重复确认！')
          return
        } else if (that.selectedData.ssfc0015 == '0' || that.selectedData.ssfc0015 == '2' || that.selectedData.ssfc0015 == '' || that.selectedData.ssfc0015 == '-1' || that.selectedData.ssfc0015 == undefined) {
          this.$Message.error('花名册保障人员未通过事后查验，不允许进行花名册确认！')
          return
        } else {
          that.Loading = true
          this.$Modal.confirm({
            title: '确认此数据的救助对象花名册后，不允许重复生成本月数据！确定当前操作吗？',
            onOk: () => {
              let record = that.selectedData
              record.ssfc0002 = DateFormat(record.ssfc0002, 'YYYY-MM')
              postAction(url, record).then((res) => {
                if (res.data.errcode != 0) {
                  this.$Message.error(res.data.errmsg)
                } else {
                  this.modalFormOk()
                  if (res.data.data.length > 0) {
                    that.dataCountNoBankInfo = res.data.data.length
                    that.noBankInfo = true
                    that.noBankInfoData = res.data.data
                  } else {
                    that.$Message.success('确认成功！')
                    that.Loading = false
                  }
                  that.visible = false
                }
              }).finally(() => {
                that.loadData()
              })
            },
            onCancel: () => {
              that.Loading = false
            }
          })
        }
      },
      searchYearMonth: function ($event) {
        this.formData.ssfc0002 = $event
        eventBus.$emit('ssfc0002', this.formData.ssfc0002)
        tableOperationReport.methods.showAll()
      },
      searchQuery18900 () {
        let that = this
        that.queryParam.ssfc0002 = DateFormat(that.queryParam.ssfc0002, 'YYYY-MM-DD')
        this.loadData()
      }
    },
    created () {
      this.loadData()
    },
  }
</script>
<style scoped>
    /deep/ .ivu-form-item-error-tip {
        display: none;
    }

    .itemStyle {
        width: 300px
    }

    .itemShortStyle {
        width: 180px
    }

    /deep/ .ivu-form-item {
        margin-bottom: 10px;
    }
</style>
